<?php
/**
 * 设置银行卡
 * User: zal
 * Date: 2016/3/25
 * Time: 15:48
 */
namespace Api\Controller;
use Api\Controller\BaseController;

class BankCardController extends BaseController {

    public function index(){

    }

    /**
     * 跳转到设置银行卡界面
     */
    public function toSet(){
        //解决跨域
        echoHeader();
        $user_id=I('get.user_id','');
        $bcid=I('get.bcid','');
        $userData=$this->checkUserIsExist($user_id);
        if(isset($userData["code"])){
            $this->ajaxReturn($userData,'JSON');
        }
        $bankCards=array();
        if(!empty($bcid)){
            $bankCards=M('BankCard')->where("id=".$bcid)->find();
        }

        $map["user_id"]=$user_id;
        $bankList=M('BankList')->where("status=1")->select();
        $provinceList=M('Province')->select();
        //$cityList=M('City')->select();

        $data["bankList"]=$bankList;
        $data["provinceList"]=$provinceList;
        $data["bankCard"]=$bankCards;
        $data["code"]=200;
        $data["msg"]='ok';
        $this->ajaxReturn($data,'JSON');
    }

    /**
     * 处理设置银行卡操作
     */
    public function doSet(){
        //解决跨域
        echoHeader();
        $bcid=I('post.bcid','');
        $user_id=I('post.user_id','');
        $bank_id=I('post.bank_id','');
        $bank_num=I('post.bank_num','');
        $province_id=I('post.province_num','');
        $city_id=I('post.city_num','');
        $isdefault=I('post.isdefault','0');
        $userData=$this->checkUserIsExist($user_id);
        if(isset($userData["code"])){
            $this->ajaxReturn($userData,'JSON');
        }

        if(empty($bank_id)||empty($bank_num)||empty($province_id)||empty($city_id)){
            $data["code"]=1001;
            $data["msg"]='缺少参数';
            $this->ajaxReturn($data,'JSON');
        }

        $bankCardModel=M('BankCard');
        $insertData["name"]="";
        $insertData["id_num"]="";
        $insertData["user_id"]=$user_id;
        $insertData["bank_id"]=$bank_id;
        $insertData["bank_num"]=$bank_num;
        $insertData["province_num"]=$province_id;
        $insertData["city_num"]=$city_id;
        $insertData["isdefault"]=$isdefault;
        $insertData["add_time"]=time();
        $result=false;
        $bankCard=$bankCardModel->where("bank_num=".$bank_num)->find();
        if(!empty($bankCard)&&!empty($bcid)){
            if($bcid!=$bankCard["id"]){
                $data["code"]=403;
                $data["msg"]='您已经设置过该银行卡号';
                $this->ajaxReturn($data,'JSON');
            }
            $result=$bankCardModel->where("id=".$bcid)->save($insertData);
        }else if(!empty($bankCard)){
            $bcid=$bankCard["id"];
            $result=$bankCardModel->where("id=".$bcid)->save($insertData);
        }else if(!empty($bcid)){
            $bankCard=$bankCardModel->find($bcid);
            if(!empty($bankCard)){
                $result=$bankCardModel->where("id=".$bcid)->save($insertData);
            }else{
                $result=$bankCardModel->add($insertData);
            }
        }else{
            $result=$bankCardModel->add($insertData);
        }

        if($result===false){
            $data["code"]=403;
            $data["msg"]='操作失败';
        }else{
            $data["code"]=200;
            $data["msg"]='ok';
        }
        $this->ajaxReturn($data,'JSON');
    }
}